package com.zhs.gccs.view.gt;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.GtArea;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.view.BaseView;

import java.util.ArrayList;
import java.util.List;

public class AreaSelectView extends BaseView {
    private TextView tv_cancel;
    private TextView tv_confirm;
    private View ll_cb_all_province;
    private CheckBox cb_province_all;

    private RecyclerView rv_province;
    private GtAreaAdapter provinceAdapter;
    private List<GtArea> provinces;
    private RecyclerView rv_city;
    private List<GtArea> cities;
    private GtAreaAdapter cityAdapter;
    private View ll_city_all;
    private CheckBox cb_city_all;

    private RecyclerView rv_district;
    private GtAreaAdapter districtAdapter;
    private List<GtArea> districts;
    private View ll_district_all;
    private CheckBox cb_district_all;

    public AreaSelectView(@NonNull Context context) {
        super(context);
    }

    public AreaSelectView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AreaSelectView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_area,
                this, true);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        ll_cb_all_province = view.findViewById(R.id.ll_cb_all_province);
        cb_province_all = view.findViewById(R.id.cb_province_all);
        tv_cancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Stream.of(gtAreas).forEach(item->item.setChecked(false));
                provinceAdapter.notifyDataSetChanged();
                cityAdapter.notifyDataSetChanged();
                districtAdapter.notifyDataSetChanged();
                cb_province_all.setChecked(false);
                cb_city_all.setChecked(false);
                cb_district_all.setChecked(false);
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(onItemChangeListener!=null)onItemChangeListener.dismis();
            }
        });
        ll_cb_all_province.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                cb_province_all.setChecked(!cb_province_all.isChecked());
                Stream.of(provinces).forEach(item->item.setChecked(cb_province_all.isChecked()));
                provinceAdapter.notifyDataSetChanged();
                if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();
            }
        });
        rv_province = view.findViewById(R.id.rv_province);
        rv_city = view.findViewById(R.id.rv_city);
        rv_district = view.findViewById(R.id.rv_district);
        tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_confirm = view.findViewById(R.id.tv_confirm);
        ll_city_all = view.findViewById(R.id.ll_city_all);
        cb_city_all = view.findViewById(R.id.cb_city_all);
        ll_district_all = view.findViewById(R.id.ll_district_all);
        cb_district_all = view.findViewById(R.id.cb_district_all);

        this.provinces = new ArrayList<>();
        rv_province.setLayoutManager(new LinearLayoutManager(context));
        provinceAdapter = new GtAreaAdapter(context, this.provinces);
        rv_province.setAdapter(provinceAdapter);
        provinceAdapter.setOperationListener(new GtAreaAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                provinceAdapter.setSelectedIndex(position);
                provinceAdapter.notifyDataSetChanged();
                GtArea province = provinces.get(position);
                List<GtArea> areas = Stream.of(gtAreas).filter(i -> i.getPid().equals(province.getId())).toList();
                cities.clear();
                if(areas!=null)cities.addAll(areas);
                cityAdapter.setSelectedIndex(-1);
                cityAdapter.notifyDataSetChanged();
                districts.clear();
                //初始化区
                if(cities!=null&&cities.size()>0){
                    List<GtArea> areaDistricts = Stream.of(gtAreas).filter(i -> i.getPid().equals(cities.get(0).getId())).toList();
                    districts.clear();
                    if(areaDistricts!=null)districts.addAll(areaDistricts);
                }
                districtAdapter.notifyDataSetChanged();

                long countP = Stream.of(provinces).filter(i -> i.isChecked()).count();
                boolean pAll = countP==provinces.size()?true:false;
                cb_province_all.setChecked(pAll);

                long countC = Stream.of(cities).filter(i -> i.isChecked()).count();
                boolean cAll = countC==cities.size()?true:false;
                cb_city_all.setChecked(cAll);

                long countD = Stream.of(districts).filter(i -> i.isChecked()).count();
                boolean dAll = countD==districts.size()?true:false;
                cb_district_all.setChecked(dAll);
            }

            @Override
            public void onItemCheckedChange(int position) {
                GtArea province = provinces.get(position);
                province.setChecked(!province.isChecked());
                provinceAdapter.notifyItemChanged(position);
                List<GtArea> areas = Stream.of(gtAreas).filter(i -> i.getPid().equals(province.getId())).toList();
                cities.clear();
                if(areas!=null)cities.addAll(areas);
                //不联动
//                for(GtArea c:cities){
//                    c.setChecked(province.isChecked());
//                }
                cityAdapter.notifyDataSetChanged();
                if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();

                long countP = Stream.of(provinces).filter(i -> i.isChecked()).count();
                boolean pAll = countP==provinces.size()?true:false;
                cb_province_all.setChecked(pAll);

                long countC = Stream.of(cities).filter(i -> i.isChecked()).count();
                boolean cAll = countC==cities.size()?true:false;
                cb_city_all.setChecked(cAll);

                long countD = Stream.of(districts).filter(i -> i.isChecked()).count();
                boolean dAll = countD==districts.size()?true:false;
                cb_district_all.setChecked(dAll);

            }
        });

        this.cities = new ArrayList<>();
        rv_city.setLayoutManager(new LinearLayoutManager(context));
        cityAdapter = new GtAreaAdapter(context, this.cities);
        rv_city.setAdapter(cityAdapter);
        cityAdapter.setOperationListener(new GtAreaAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
                cityAdapter.setSelectedIndex(position);
                cityAdapter.notifyDataSetChanged();
                GtArea city = cities.get(position);
                List<GtArea> areas = Stream.of(gtAreas).filter(i -> i.getPid().equals(city.getId())).toList();
                districts.clear();
                if(areas!=null)districts.addAll(areas);
                districtAdapter.notifyDataSetChanged();

                long countC = Stream.of(cities).filter(i -> i.isChecked()).count();
                boolean cAll = countC==cities.size()?true:false;
                cb_city_all.setChecked(cAll);

                long countD = Stream.of(districts).filter(i -> i.isChecked()).count();
                boolean dAll = countD==districts.size()?true:false;
                cb_district_all.setChecked(dAll);
            }
            @Override
            public void onItemCheckedChange(int position) {
                GtArea city = cities.get(position);
                city.setChecked(!city.isChecked());
                cityAdapter.notifyItemChanged(position);
                List<GtArea> areas = Stream.of(gtAreas).filter(i -> i.getPid().equals(city.getId())).toList();
                districts.clear();
                if(areas!=null)districts.addAll(areas);
                //不联动
//                for(GtArea c:districts){
//                    c.setChecked(city.isChecked());
//                }
                districtAdapter.notifyDataSetChanged();
                if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();

                long countC = Stream.of(cities).filter(i -> i.isChecked()).count();
                boolean cAll = countC==cities.size()?true:false;
                cb_city_all.setChecked(cAll);

                long countD = Stream.of(districts).filter(i -> i.isChecked()).count();
                boolean dAll = countD==districts.size()?true:false;
                cb_district_all.setChecked(dAll);
            }
        });
        ll_city_all.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                cb_city_all.setChecked(!cb_city_all.isChecked());
                Stream.of(cities).forEach(item->item.setChecked(cb_city_all.isChecked()));
                cityAdapter.notifyDataSetChanged();
                if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();
            }
        });


        this.districts = new ArrayList<>();
        rv_district.setLayoutManager(new LinearLayoutManager(context));
        districtAdapter = new GtAreaAdapter(context, this.districts);
        rv_district.setAdapter(districtAdapter);
        districtAdapter.setOperationListener(new GtAreaAdapter.OperationListener() {
            @Override
            public void onItemClick(int position) {
            }
            @Override
            public void onItemCheckedChange(int position) {
                GtArea district = districts.get(position);
                district.setChecked(!district.isChecked());
                districtAdapter.notifyItemChanged(position);
                if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();

                long countD = Stream.of(districts).filter(i -> i.isChecked()).count();
                boolean dAll = countD==districts.size()?true:false;
                cb_district_all.setChecked(dAll);
            }
        });
        ll_district_all.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                cb_district_all.setChecked(!cb_district_all.isChecked());
                Stream.of(districts).forEach(item->item.setChecked(cb_district_all.isChecked()));
                districtAdapter.notifyDataSetChanged();
                if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();
            }
        });


        tv_cancel.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(gtAreas!=null){
                    Stream.of(gtAreas).forEach(i -> i.setChecked(false));
                    provinceAdapter.notifyDataSetChanged();
                    cityAdapter.notifyDataSetChanged();
                    districtAdapter.notifyDataSetChanged();
                    if(onItemChangeListener!=null)onItemChangeListener.onItemChanged();
                }
            }
        });
        tv_confirm.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(onItemChangeListener!=null)onItemChangeListener.dismis();
            }
        });
    }

    List<GtArea> gtAreas;
    public void initData(List<GtArea> gtAreas) {
        if(gtAreas==null)return;
        this.gtAreas = gtAreas;
        List<GtArea> areas = Stream.of(this.gtAreas).filter(i -> i.getPid().equals("0")).toList();
        this.provinces.clear();
        this.provinces.addAll(areas);
        this.provinceAdapter.notifyDataSetChanged();
        cb_province_all.setChecked(false);
        cb_city_all.setChecked(false);
        cb_district_all.setChecked(false);
        //初始化市
        if(this.provinces!=null&&this.provinces.size()>0){
            //初始化区
            List<GtArea> areaCities = Stream.of(gtAreas).filter(i -> i.getPid().equals(provinces.get(0).getId())).toList();
            cities.clear();
            if(areaCities!=null)cities.addAll(areaCities);
            cityAdapter.notifyDataSetChanged();
        }
        if(this.cities!=null&&this.cities.size()>0){
            List<GtArea> areaDistricts = Stream.of(gtAreas).filter(i -> i.getPid().equals(cities.get(0).getId())).toList();
            districts.clear();
            if(areaDistricts!=null)districts.addAll(areaDistricts);
            districtAdapter.notifyDataSetChanged();
        }

    }

    public List<GtArea> getSelectedItems() {
        if(this.gtAreas==null)return null;
        List<GtArea> areas = Stream.of(this.gtAreas)
                .filter(i -> i.isChecked())
                .toList();
        return areas;
    }

    public List<String> getSelectedValue() {
        return Stream.of(this.gtAreas)
                .filter(i -> i.isChecked())
                .map(i->i.getCode())
                .toList();
    }

    private OnItemChangeListener onItemChangeListener;

    public void setOnItemChangeListener(OnItemChangeListener onItemChangeListener) {
        this.onItemChangeListener = onItemChangeListener;
    }



    public interface OnItemChangeListener{
        void onItemChanged();

        void dismis();
    }
}
